package com.z.kq.controller;

import com.github.pagehelper.PageInfo;
import com.z.kq.entity.OfficialBusiness;
import com.z.common.web.base.BaseController;
import com.z.common.web.vo.request.PageVo;
import com.z.common.aop.annotation.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import com.z.common.aop.enums.BusinessType;
import com.z.common.web.vo.response.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.z.kq.service.IOfficialBusinessService;
import org.springframework.web.servlet.ModelAndView;
import java.util.Arrays;

/**
 * 公出Controller
 *
 * @author z
 * @date 2023-03-30
 */
@RestController
@Api(tags = "公出管理")
@RequestMapping("/kq/officialBusiness")
public class OfficialBusinessController extends BaseController {

    private String prefix = "kq/officialBusiness";

    @Autowired
    private IOfficialBusinessService officialBusinessService;

    @ApiOperation(value = "查询页面")
    @GetMapping("/main")
    @PreAuthorize("hasPermission('/kq/officialBusiness/main','kq:officialBusiness:main')")
    public ModelAndView main() {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询公出列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/kq/officialBusiness/data','kq:officialBusiness:main')")
    @Log(title = "公出", describe = "查询公出列表", type = BusinessType.QUERY)
    @ApiOperation(value = "查询接口")
    public Result list(@ModelAttribute OfficialBusiness officialBusiness, PageVo pageVo) {
        PageInfo<OfficialBusiness> pageInfo = officialBusinessService.selectOfficialBusinessPage(officialBusiness, pageVo);
        return Result.table(pageInfo);
    }

    /**
     * 新增公出
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/kq/officialBusiness/add','kq:officialBusiness:add')")
    @ApiOperation(value = "添加页面")
    public ModelAndView add() {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增公出
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/kq/officialBusiness/save','kq:officialBusiness:add')")
    @Log(title = "公出", describe = "新增公出数据", type = BusinessType.ADD)
    @ApiOperation(value = "添加接口")
    public Result save(@RequestBody OfficialBusiness officialBusiness) {
        return Result.decide(officialBusinessService.save(officialBusiness));
    }

    /**
     * 修改公出
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/kq/officialBusiness/edit','kq:officialBusiness:edit')")
    @ApiOperation(value = "修改页面")
    public ModelAndView edit(Long id, ModelMap map) {
        OfficialBusiness officialBusiness =officialBusinessService.getById(id);
        map.put("officialBusiness", officialBusiness);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改公出
     */
    @ResponseBody
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/kq/officialBusiness/update','kq:officialBusiness:edit')")
    @Log(title = "公出", describe = "修改公出数据", type = BusinessType.EDIT)
    @ApiOperation(value = "修改接口")
    public Result update(@RequestBody OfficialBusiness officialBusiness) {
        officialBusiness.setUsername(null);
        return Result.decide(officialBusinessService.updateById(officialBusiness));
    }

    /**
     * 删除公出
     */
    @ResponseBody
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasPermission('/kq/officialBusiness/batchRemove','kq:officialBusiness:del')")
    @Log(title = "公出", describe = "删除公出数据", type = BusinessType.REMOVE)
    @ApiOperation(value = "批量删除接口")
    public Result batchRemove(String ids) {
        return Result.decide(officialBusinessService.removeByIds(Arrays.asList(ids.split(","))));
    }

    /**
     * 删除公出
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/kq/officialBusiness/remove','kq:officialBusiness:del')")
    @Log(title = "公出", describe = "删除公出数据", type = BusinessType.REMOVE)
    @ApiOperation(value = "删除接口")
    public Result remove(@PathVariable("id") Long id) {
        return Result.decide(officialBusinessService.removeById(id));
    }
}
